$(function () {
    if(slf.checkSign()){
        //登录
        //推广信息请求
        requestSpread();
    }else{
        //未登录
        window.location.href= "noLogin.html";
    }
    //注销
    logout();

})


/**
 * 用户基本信息和孩子信息请求
 * @param data
 */
function requestChildren(data) {
    slf.ajax({
        url: '/myCenter/getUserInfo',
        success: function (data) {
        	var childMess = slf.getSequenceChild();
        	if(data!=undefined&&data!=null){
                //赋值给用户基本信息
                person(data);
                //账户资料
                acountData(data.obj.userId)
                //推广样式
                spreadStyle(data.obj.recommendId)
            }
           if(childMess!=undefined&&childMess!=null){
               //赋值给孩子基本信息
               childrenInfo(childMess)
           }else{
        	   var html="";
        	   html += "<li id='coursePageId' class=\"couseDivLi\">"
                   + " <span class=\"courseName\">暂无进程课程</span>"
                   + "</li>"
	           $("#coursePage").append(html);
	        	   $("#headImg").attr('src', '../../img/noChild.jpg')
	           }
        }
    });
}
/**
 * 推广请求
 */
function requestSpread() {
    slf.ajax({
        url: '/recommendMoney/effectiveNumber',
        success: function (data) {
            if (data.res > 0) {
                //推广赋值
                spread(data);
            }
        }
    });
}
/**
 * 基本信息
 * @param data
 */
function person(data) {
    var info = ""
    userId = data.obj.userId;
    phoneUser = data.obj.phone;
    nameUser = data.obj.name;
    if (data.res > 0) {
        sessionStorage.setItem("lfb", data.obj.lfb)
        var childMess=slf.getSequenceChild();
        if (childMess!=null&&childMess.vipLv!=undefined&&childMess.vipLv!=null&&childMess.vipLv==1) {
            //vip用户
            $("#vipDiv").removeClass("am-hide")
        }
        $("#person").attr("data-userId",data.obj.userId)
        $("#myLfb span").html(data.obj.lfb)
    }
}
//账户资料
function acountData(userId){
    $("#person").click(function(){
        var userId=$(this).attr("data-userId")
        window.location.href='index_edit.html?userId=' + userId
    })

}

/**
 * 推广样式
 */
function spreadStyle(qrCode){
    if(qrCode!=undefined&&qrCode!=null){
        $(".imgCodeDiv").click(function(){
            window.location.href='extension.html?qrCode='+qrCode+'&imgId='+$(this).attr('id')
        })
    }else{
        $(".imgCodeDiv").click(function(){
            alert("你还没有推广码")
        })
    }

}
/**
 * 推广赋值
 * @param data
 */
function spread(data) {
    $('.promotionNum').text(data.obj[0]);
    $('.promotionMoney').text(data.obj[1]);
    $(".promotionMoneys").text(data.obj[2])
    //孩子基本信息请求
    requestChildren()
}
/**
 * 赋值孩子头像以及课程信息
 * @param data
 */
function childrenInfo(data) {
    //孩子头像
    if (checkNull(data.headPortrait)) {
    	if(data.headPortrait=="../img/boy.png"){
    		$("#headImg").attr('src', '../'+data.headPortrait)
    	}else{
    		$("#headImg").attr('src',data.headPortrait)
    	}
        
    }
    //孩子名称
    if (checkNull(data.name)) {
    	$(".childName").removeAttr("onclick")
    	$(".childName").html('<span id="childName">'+data.name+'</span>')
    }else{
    	$(".childName").html('<span id="childName" style="font-size:12px;color:#39A4F4;">完善孩子信息</span>')
    }
    //孩子课程包
    var page = data.childrenCoursePage;
    //孩子课程
    var course = data.schoolCoursePage;
    var html = "";
	if (page.length >= 2) {
        $.each(page, function (index, values) {
        	if(index<2){
        		html += "<li id='" + values.coursePageId + "' class=\"couseDivLi\">"
                + " <span class=\"courseName\">" + values.name + "</span>"
                + "<span  class=\"courseNum\">（剩余" + values.count + "节课）</span>"
                + "</li>"
        	}
        })
        html += " <li class=\"couseMoreLi\" onclick=\"javascript:window.location.href='curProgress.html?childrenId="+data.childrenId+"'\">\n" +
        "\t                <span class=\"moreBtn\">查看更多</span>\n" +
        "\t             </li>"
    } else if (page.length == 1) {
        html += "<li id='" + page[0].coursePageId + "' class=\"couseDivLi\">"
            + " <span class=\"courseName\">" + page[0].name + "</span>"
            + "<span  class=\"courseNum\">（剩余" + page[0].count + "节课）</span>"
            + "</li>"
        if(course.length>0){
        	html += "<li id='" + course[0].courseId + "' class=\"couseDivLi\">"
            + " <span class=\"courseName\">" + course[0].name + "</span>"
            + "<span  class=\"courseNum\">（共1节课）</span>"
            + "</li>"
        }
        html += " <li class=\"couseMoreLi\" onclick=\"javascript:window.location.href='curProgress.html?childrenId="+data.childrenId+"'\">\n" +
        "\t                <span class=\"moreBtn\">查看更多</span>\n" +
        "\t             </li>"
    } else if (page.length == 0) {
    	if(course.length>0){
    		$.each(course, function (index, values) {
    			if(index<2){
    				 html += "<li id='" + values.courseId + "' class=\"couseDivLi\">"
                     + " <span class=\"courseName\">" + values.name + "</span>"
                     + "<span  class=\"courseNum\">（共1节课）</span>"
                     + "</li>"
    			}
            })
            html += " <li class=\"couseMoreLi\" onclick=\"javascript:window.location.href='curProgress.html?childrenId="+data.childrenId+"'\">\n" +
            "\t                <span class=\"moreBtn\">查看更多</span>\n" +
            "\t             </li>"
    	}else{
    		 html += "<li id='coursePageId' class=\"couseDivLi\">"
    	            + " <span class=\"courseName\" style='text-align:left;'>暂无进程课程</span>"
    	            + "</li>"
    	}
    } else {
        html += "<li id='coursePageId' class=\"couseDivLi\">"
            + " <span class=\"courseName\">暂无进程课程</span>"
            + "</li>"
    }
    $("#coursePage").append(html);
   
}
/**
 * 注销
 */
function logout(){
    $('#cancellation').click(function () {
    	$("#class_model .am-modal-bd").html("是否确定注销此账号？");
    	$("#class_model").modal({
    		closeViaDimmer: false,
    		onConfirm: function(options) {
    			slf.ajax({
    	            url: '/userLogout',
    	            success: function (data) {
    	                console.log(data)
    	                console.log("注销")
    	                if (data.res == 0) {
    	                   window.location.href = '../login.html';
    	                }
    	            }
    	        });
	        },
    	})
    });
}

/**
 * 判断为空
 * @param str
 * @returns {boolean}
 */
function checkNull(str) {
    if (str != null && str != undefined) {
        return true;
    } else {
        return false
    }
}